package com.openingedu.mba.persist;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

public class DataHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "MBAEXAM.db";
	private static final int DATABASE_VERSION = 15;

	private Dao<DbProfile, Long> profileDao = null;
	private Dao<PersonalSettings, Long> personalSettingsDao = null;
	private Dao<DbMessage, Long> dbMessageDao = null;
	private Dao<DbPaper, Long> dbPaperDao = null;
	private Dao<DbPaperQuestion, Long> dbPaperQuestionDao = null;
	private Dao<DbQuestion, Long> dbQuestionDao = null;
	private Dao<DbQuestionOption, Long> dbQuestionOptionDao = null;
	private Dao<DbQuestionSeckill, Long> dbQuestionSeckillDao = null;
	private Dao<DbQuestionSolution, Long> dbQuestionSolutionDao = null;

	private Dao<DbFaultQuestion, Long> dbFaultQuestionDao = null;

	private Dao<DbHistory, Long> historyDao = null;
	
	private Dao<DbOrder, Long> orderDao = null;

	public DataHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			TableUtils.createTable(connectionSource, DbProfile.class);
			TableUtils.createTable(connectionSource, PersonalSettings.class);
			TableUtils.createTable(connectionSource, DbMessage.class);
			TableUtils.createTable(connectionSource, DbPaper.class);
			TableUtils.createTable(connectionSource, DbPaperQuestion.class);
			TableUtils.createTable(connectionSource, DbQuestion.class);
			TableUtils.createTable(connectionSource, DbQuestionOption.class);
			TableUtils.createTable(connectionSource, DbQuestionSeckill.class);
			TableUtils.createTable(connectionSource, DbQuestionSolution.class);
			TableUtils.createTable(connectionSource, DbFaultQuestion.class);
			TableUtils.createTable(connectionSource, DbHistory.class);
			TableUtils.createTable(connectionSource, DbOrder.class);
		} catch (SQLException e) {
			Log.e(DataHelper.class.getName(), "create datebase error", e);
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int arg2, int arg3) {
		try {
			TableUtils.dropTable(connectionSource, DbProfile.class, true);
			TableUtils.dropTable(connectionSource, PersonalSettings.class, true);
			TableUtils.dropTable(connectionSource, DbMessage.class, true);
			TableUtils.dropTable(connectionSource, DbPaper.class, true);
			TableUtils.dropTable(connectionSource, DbPaperQuestion.class, true);
			TableUtils.dropTable(connectionSource, DbQuestion.class, true);
			TableUtils.dropTable(connectionSource, DbQuestionOption.class, true);
			TableUtils.dropTable(connectionSource, DbQuestionSeckill.class,true);
			TableUtils.dropTable(connectionSource, DbQuestionSolution.class,true);
			TableUtils.dropTable(connectionSource, DbFaultQuestion.class, true);
			TableUtils.dropTable(connectionSource, DbHistory.class, true);
			TableUtils.dropTable(connectionSource, DbOrder.class, true);
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(DataHelper.class.getName(), "upgrade database error", e);
		}
	}

	@Override
	public void close() {
		super.close();
		profileDao = null;
		personalSettingsDao = null;
		dbMessageDao = null;
		dbPaperDao = null;
		dbPaperQuestionDao = null;
		dbQuestionDao = null;
		dbQuestionOptionDao = null;
		dbQuestionSeckillDao = null;
		dbQuestionSolutionDao = null;

		dbFaultQuestionDao = null;
		historyDao = null;
		orderDao = null;
	}
	
	public Dao<DbOrder, Long> getOrderDao() throws SQLException {
		if (orderDao == null) {
			orderDao = getDao(DbOrder.class);
		}
		return orderDao;
	}

	public Dao<DbHistory, Long> getHistoryDao() throws SQLException {
		if (historyDao == null) {
			historyDao = getDao(DbHistory.class);
		}
		return historyDao;
	}

	public Dao<DbFaultQuestion, Long> getDbFaultQuestionDao()
			throws SQLException {
		if (dbFaultQuestionDao == null) {
			dbFaultQuestionDao = getDao(DbFaultQuestion.class);
		}
		return dbFaultQuestionDao;
	}

	public Dao<PersonalSettings, Long> getPersonalSettingsDao()
			throws SQLException {
		if (personalSettingsDao == null) {
			personalSettingsDao = getDao(PersonalSettings.class);
		}
		return personalSettingsDao;
	}

	public Dao<DbPaperQuestion, Long> getDbPaperQuestionDao()
			throws SQLException {
		if (dbPaperQuestionDao == null) {
			dbPaperQuestionDao = getDao(DbPaperQuestion.class);
		}
		return dbPaperQuestionDao;
	}

	public Dao<DbProfile, Long> getProfileDao() throws SQLException {
		if (profileDao == null) {
			profileDao = getDao(DbProfile.class);
		}
		return profileDao;
	}

	public Dao<DbMessage, Long> getDbMessageDao() throws SQLException {
		if (dbMessageDao == null) {
			dbMessageDao = getDao(DbMessage.class);
		}
		return dbMessageDao;
	}

	public Dao<DbPaper, Long> getDbPaperDao() throws SQLException {
		if (dbPaperDao == null) {
			dbPaperDao = getDao(DbPaper.class);
		}
		return dbPaperDao;
	}

	public Dao<DbQuestion, Long> getDbQuestionDao() throws SQLException {
		if (dbQuestionDao == null) {
			dbQuestionDao = getDao(DbQuestion.class);
		}
		return dbQuestionDao;
	}

	public Dao<DbQuestionOption, Long> getDbQuestionOptionDao()
			throws SQLException {
		if (dbQuestionOptionDao == null) {
			dbQuestionOptionDao = getDao(DbQuestionOption.class);
		}
		return dbQuestionOptionDao;
	}

	public Dao<DbQuestionSeckill, Long> getDbQuestionSeckillDao()
			throws SQLException {
		if (dbQuestionSeckillDao == null) {
			dbQuestionSeckillDao = getDao(DbQuestionSeckill.class);
		}
		return dbQuestionSeckillDao;
	}

	public Dao<DbQuestionSolution, Long> getDbQuestionSolutionDao()
			throws SQLException {
		if (dbQuestionSolutionDao == null) {
			dbQuestionSolutionDao = getDao(DbQuestionSolution.class);
		}
		return dbQuestionSolutionDao;
	}
}